package com.sakura.二叉树;

public class Code404_左叶子之和 {

    // Definition for a binary tree node.
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null)
            return 0;

        int sum = sumOfLeftLeaves(root.left);
        if (root.left != null && root.left.left == null && root.left.right == null) {
            sum += root.left.val;
        }
        sum += sumOfLeftLeaves(root.right);
        return sum;
    }

    public int sumOfLeftLeaves_1(TreeNode root) {
        return dfs(root.left, true) + dfs(root.right, false);
     }
 
     public int dfs(TreeNode node, boolean flag) {
         if (node == null) {return 0;}
         
         if (node.left == null && node.right == null && flag) {
             return node.val;
         }
         int sum = dfs(node.left, true);
         sum +=dfs(node.right, false);
         return sum;
     }
}